package com.nervenets.general.hibernate;

import com.nervenets.general.adapter.jsonType.LongArrayJson;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.Type;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * 2020/6/30 11:36 created by Joe
 **/
@EqualsAndHashCode(callSuper = true)
@MappedSuperclass
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public abstract class TreeDomainObject<T extends TreeDomainObject<T>> extends DomainObject {
    @ApiModelProperty("名称")
    @Column(columnDefinition = "varchar(191)")
    private String name;
    @ApiModelProperty("上级ID")
    private long parentId;
    @ApiModelProperty("上级IDs")
    @Type(type = LongArrayJson.NAME)
    @Column(columnDefinition = "json")
    @Builder.Default
    private List<Long> parentIds = new ArrayList<>();
    @ApiModelProperty("排序值")
    private int queue;
    @ApiModelProperty("是否启用")
    @Builder.Default
    private boolean enable = true;
    @ApiModelProperty("子级列表")
    @Transient
    private List<T> children;

    public void addChild(T object) {
        if (null == this.children) this.children = new ArrayList<>();
        this.children.add(object);
    }
}
